
package BOH.GUI;

import BOH.data.TableConnection;
import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;

/**
 * The Back of House System for a
 * small restaurant.
 * 
 * Maeghan Colvin & Jennifer Haveman
 * CSC 289 0B01
 * February 13, 2014
 */

public class ViewTotalSalesPanel extends JPanel
{
    // Variables
    private JFrame frame;
    private JPanel mainPanel;               // Holds the mainPanel
    private JPanel optionsPanel;            // Holds the optionsPanel
    private JPanel buttonPanel;             // Holds the buttonPanel
    private JPanel tempPanel;               // Holds tempPanel for formatting
    private JLabel startingDateLabel;       // Holds the startingDateLabel
    private JLabel endingDateLabel;         // Holds endingDateLabel
    private JSpinner startingDateSpinner;   // Holds startingDateSpinner
    private JSpinner endingDateSpinner;     // Holds endingDateSpinner
    private SpinnerDateModel model;         // Holds the model for startingDate
    private SpinnerDateModel model2;        // Holds the model for endingDate
    private String startingDate;            // Holds the startingDate
    private String endingDate;              // Holds the endingDate 
    private SimpleDateFormat dateFormat;    // Holds the date format
    private JButton viewButton;             // Views the payroll results
    private String sqlStatement;            // Selects total sales
    
    /**
     * Constructor
     */
    
    public ViewTotalSalesPanel()
    {
        // Set Look and Feel.
        JFrame.setDefaultLookAndFeelDecorated(true);
        
        // Create and set up the window.
        frame = new JFrame("Total Sales");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        
        // Build and add mainPanel
        this.buildMainPanel();
        frame.add(mainPanel);
        
        // Add content to the window.
        frame.setSize(190, 200);
        frame.validate();
        
        // Display the window.
        frame.setVisible(true);  
    }
    
    /**
     * The buildMainPanel builds the 
     * mainPanel.
     */
    
    private void buildMainPanel() 
    {
        // Create mainPanel
        mainPanel = new JPanel(new BorderLayout());
        mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        
        // Get optionsPanel
        buildOptionsPanel();
        
        // Get buttonPanel
        buildButtonPanel();
        
        // Add panels
        mainPanel.add(optionsPanel, BorderLayout.CENTER);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);
    }
    
     /**
     * The buildOptionsPanel builds the 
     * optionsPanel.
     */
    
    private void buildOptionsPanel() 
    {
        // Create optionsPanel
        optionsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        optionsPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        
        // Create optionsPanel
        tempPanel = new JPanel(new GridLayout(8, 0));
        tempPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        
        // Create the JLabels
        startingDateLabel = new JLabel("Starting Date: ");
        endingDateLabel = new JLabel("Ending Date: ");
        
        // Create dates for spinners
        dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        Date initDate = calendar.getTime();
        calendar.add(Calendar.YEAR, -100);
        Date earliestDate = calendar.getTime();
        calendar.add(Calendar.YEAR, 200);
        Date latestDate = calendar.getTime();
                
        // Create Spinners model
        model = new SpinnerDateModel(initDate, //initial value
                               earliestDate, //min
                               latestDate, //max
                               Calendar.YEAR);                //step
        
        // Create Spinners model
        model2 = new SpinnerDateModel(initDate, //initial value
                               earliestDate, //min
                               latestDate, //max
                               Calendar.YEAR);                //step
        
        // Create spinner
        startingDateSpinner = new JSpinner(model);
        startingDateSpinner.setToolTipText("Select a starting date");
        startingDateSpinner.setEditor(new JSpinner.DateEditor(startingDateSpinner, dateFormat.toPattern()));
        startingDateSpinner.addChangeListener(new ViewTotalSalesPanel.StartingDateSpinnerListener());
        
        // Create spinner
        endingDateSpinner = new JSpinner(model2);
        endingDateSpinner.setToolTipText("Select a an ending date");
        endingDateSpinner.setEditor(new JSpinner.DateEditor(endingDateSpinner, dateFormat.toPattern()));
        endingDateSpinner.addChangeListener(new ViewTotalSalesPanel.EndingDateSpinnerListener());
        
        // Add components to panel
        tempPanel.add(startingDateLabel);
        tempPanel.add(startingDateSpinner);
        tempPanel.add(endingDateLabel);
        tempPanel.add(endingDateSpinner);
        optionsPanel.add(tempPanel);
    }
    
    /**
     * The buildButtonPanel builds the 
     * buttonPanel.
     */
    
    private void buildButtonPanel() 
    {
        // Create buttonPanel
        buttonPanel = new JPanel(new BorderLayout());
        buttonPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        
        // Create button
        viewButton = new JButton("View");
        viewButton.setMnemonic(KeyEvent.VK_V);
        viewButton.addActionListener(new ViewButtonActionListener());
        
        // Add components to buttonPanel
        buttonPanel.add(viewButton, BorderLayout.CENTER);
    }
    
    /**
     * The getSalesTotals method gets the total
     * sales for the selected dates and returns them.
     * @return
     */
    
    public void getSalesTotals()
    {
        sqlStatement = "SELECT SUM(SalesPrice) AS SalesPrice, " +
                       "SUM(Tax) AS Tax, SUM(Total) AS Total " +
                       "FROM Sales " + 
                       "WHERE PayDate BETWEEN DATE('" + startingDate + "') " +
                       "AND DATE('" + endingDate + "') ";
        
        frame.setVisible(false);
        TableConnection dbQuery = new TableConnection(sqlStatement);
        String[] columnNames = dbQuery.getColumnNames();
        String[][] tableData = dbQuery.getTableData();
        GetTableResults table = new GetTableResults(tableData, columnNames);
    }
    
    /*
     * The startingDateSpinnerListener is used to 
     * retrieve the starting date for sales.
     */
    
    private class StartingDateSpinnerListener implements ChangeListener 
    {
        @Override
        public void stateChanged(ChangeEvent e) 
        {

                startingDateSpinner = (JSpinner)(e.getSource());
                startingDate = dateFormat.format(startingDateSpinner.getValue());
        }
    }
    
    /*
     * The EndingDateSpinnerListener is used to 
     * retrieve the ending date for sales.
     */
    
    private class EndingDateSpinnerListener implements ChangeListener 
    {
        @Override
        public void stateChanged(ChangeEvent e) 
        {

                endingDateSpinner = (JSpinner)(e.getSource());
                endingDate = dateFormat.format(endingDateSpinner.getValue());
        }
    }
    
    /*
     * The ViewButtonActionListener is used to
     * view the total sales information.
     */
    
    private class ViewButtonActionListener implements ActionListener 
    {
        @Override
        public void actionPerformed(ActionEvent e) 
        {
            // Retrieve the sales information.
            getSalesTotals();
        }
    }
}
